package com.snail.snailvr.views.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.snail.snailvr.R;
import com.snail.snailvr.model.InputItem;
import com.snail.snailvr.utils.Util;
import com.snail.snailvr.widget.FlexibleDividerDecoration;
import com.snail.snailvr.widget.HorizontalDividerItemDecoration;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Input界面Adapter
 * Created by snail on 16/4/22.
 */
public class InputAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements
        FlexibleDividerDecoration.PaintProvider,
        FlexibleDividerDecoration.VisibilityProvider,
        HorizontalDividerItemDecoration.MarginProvider {
    public static final int VIEW_TYPE_NORMAL = 0, VIEW_TYPE_WITH_ICON = 1, VIEW_TYPE_DIVIDER = 2;
    public static final int INPUT_COUNT = 5;

    private List<InputItem> mInputItemList;
    private LayoutInflater mLayoutInflater;
    private Paint dividerPaint;
    private int dividerMarginLeft;
    private Paint mtransparentPaint;

    public InputAdapter(Context context, List<InputItem> inputItemList) {
        mInputItemList = inputItemList;
        mLayoutInflater = LayoutInflater.from(context);
        dividerPaint = Util.getDividerPaint();
        mtransparentPaint = new Paint();
        dividerMarginLeft = context.getResources().getDimensionPixelSize(R.dimen.input_divider_margin_left);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_NORMAL:
                return new InputNormalViewHolder(mLayoutInflater.inflate(R.layout.input_item_layout, parent, false));
            case VIEW_TYPE_DIVIDER:
                return new InputDividerViewHolder(mLayoutInflater.inflate(R.layout.input_item_divider, parent, false));
            default:
                return new InputWithIconViewHolder(mLayoutInflater.inflate(R.layout.input_layout_with_icon, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof InputNormalViewHolder) {
            InputNormalViewHolder inputNormalViewHolder = (InputNormalViewHolder) holder;
            InputItem inputItem = getItem(position);
            if (inputItem != null) {
                inputNormalViewHolder.itemView.setOnClickListener(inputItem.getOnClickListener());
                inputNormalViewHolder.mTitle.setText(inputItem.getTitle());
                inputNormalViewHolder.mMore.setImageResource(R.drawable.ic_more);
            }
        } else if (holder instanceof InputWithIconViewHolder) {
            InputWithIconViewHolder inputNormalViewHolder = (InputWithIconViewHolder) holder;
            InputItem inputItem = getItem(position);
            if (inputItem != null) {
                inputNormalViewHolder.itemView.setOnClickListener(inputItem.getOnClickListener());
                inputNormalViewHolder.mTitle.setText(inputItem.getTitle());
                inputNormalViewHolder.mSubTitle.setText(inputItem.getSubTitle());
                inputNormalViewHolder.mInputIcon.setImageResource(inputItem.getIconResId());
                inputNormalViewHolder.mMore.setImageResource(R.drawable.ic_more);
            }
        } else if (holder instanceof InputDividerViewHolder) {
            InputDividerViewHolder inputNormalViewHolder = (InputDividerViewHolder) holder;
            InputItem inputItem = getItem(position);
            if (inputItem != null) {
                inputNormalViewHolder.mDividerView.setText(inputItem.getTitle());
            }
        }
    }

    @Override
    public int getItemCount() {
        return mInputItemList.size();
    }

    public InputItem getItem(int position) {
        if (position < getItemCount())
            return mInputItemList.get(position);
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        InputItem inputItem = getItem(position);
        if (inputItem != null)
            return inputItem.getInputType();
        return -1;
    }

    @Override
    public boolean shouldHideDivider(int position, RecyclerView parent) {
        if (getItemCount() == 5) {
            if (position==1||position == 2)
                return true;
        } else if (getItemCount() == 6) {
            if (position==1||position == 2 ||position == 3 || position == 4)
                return true;
        }
        return false;
    }

    @Override
    public int dividerLeftMargin(int position, RecyclerView parent) {
        if(position==0)
            return 0;
        return dividerMarginLeft;
    }

    @Override
    public int dividerRightMargin(int position, RecyclerView parent) {
        return 0;
    }


    @Override
    public Paint dividerPaint(int position, RecyclerView parent) {
        if (position==1||position == 2||position==4)
            return mtransparentPaint;
        return dividerPaint;
    }


    static class InputNormalViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.input_title)
        TextView mTitle;
        @Bind(R.id.input_more)
        ImageView mMore;

        public InputNormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class InputWithIconViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.input_title)
        TextView mTitle;
        @Bind(R.id.input_subtitle)
        TextView mSubTitle;
        @Bind(R.id.input_icon)
        ImageView mInputIcon;
        @Bind(R.id.input_more)
        ImageView mMore;

        public InputWithIconViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class InputDividerViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.input_divider_view)
        TextView mDividerView;

        public InputDividerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
